package com.it.commicity.config.longToString;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

@Configuration
public class JacksonConfig {

    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jacksonCustomizer() {
        return builder -> {
            // 全局 Long -> String
            builder.serializerByType(Long.class, new ToStringSerializer());

            // 全局 String -> Long
            builder.deserializerByType(Long.class, new StdDeserializer<Long>(Long.class) {
                @Override
                public Long deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
                    return Long.parseLong(p.getValueAsString());
                }
            });
        };
    }
}
